package zz.app.jmhviewer;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Comparator;

import zz.app.jmhviewer.JMH.JMHJsonData;

/**
 * Created by zz on 2018/1/31 21:44.
 */

public class MusicListAdapter extends BaseAdapter {
    private ArrayList<JMHJsonData> jsonData;
    private Context context;
    private LayoutInflater inflater;
    public MusicListAdapter(Context c){
        context=c;
        jsonData=new ArrayList<>();
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return jsonData.size();
    }

    @Override
    public Object getItem(int position) {
        return jsonData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if(convertView==null) {
            convertView=inflater.inflate(R.layout.musiclistlayout,parent,false);
            holder=new ViewHolder();
            holder.title=convertView.findViewById(R.id.tvtitle);
            holder.artist=convertView.findViewById(R.id.tvartist);
            convertView.setTag(holder);

        }else{
            holder=(ViewHolder)convertView.getTag();
        }
        String s=jsonData.get(position).name;
        int i=s.indexOf("-");
        String artist=s.substring(0,i);
        String title=s.substring(i+1,s.length());
        holder.title.setText(title);
        holder.artist.setText(artist);
        return convertView;



    }
    public void setJsonData(ArrayList<JMHJsonData> data){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            data.sort((o1, o2) -> o2.name.compareTo(o1.name));
        }
        jsonData=data;
    }
    static class ViewHolder{
        TextView title;
        TextView artist;
    }
}
